"use strict";
cc._RF.push(module, 'e7c96T+hWJKQ6YZNoBA6s+r', 'level_relationalGraphBase');
// scripts/levelBundles/level_RelationalGraph/level_relationalGraphBase.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
var LevelBase_1 = require("../../Game/Base/LevelBase");
var level_relationalGraphRoleBase_1 = require("./level_relationalGraphRoleBase");
var level_relationalGraphItemBase_1 = require("./level_relationalGraphItemBase");
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var level_relationGraphBase = /** @class */ (function (_super) {
    __extends(level_relationGraphBase, _super);
    function level_relationGraphBase() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.itemScrollView = null;
        _this.item = null;
        _this.bgNode = null;
        _this.roleContent = null;
        _this.vaildNodeContent = null;
        _this.lbStepCount = null;
        _this.stepCount = 20;
        return _this;
    }
    level_relationGraphBase.prototype.start = function () {
        this.loadGame();
        this.bgAddEvent();
        this.refrenshStep();
    };
    level_relationGraphBase.prototype.loadGame = function () {
        return __awaiter(this, void 0, void 0, function () {
            var allCount, curCount, needCount, creatorItem, i, _loop_1, i;
            var _this = this;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        allCount = this.roleContent.childrenCount;
                        curCount = this.vaildNodeContent.childrenCount;
                        needCount = allCount - curCount;
                        creatorItem = function (item, index) {
                            var nNode = _this.roleContent.children[index];
                            var ns = nNode.getComponent(level_relationalGraphRoleBase_1.default);
                            var is = item.getComponent(level_relationalGraphItemBase_1.default);
                            is.bindData(ns.getRoleId(), ns.getRoleName());
                        };
                        for (i = 0; i < curCount; i++) {
                            creatorItem(this.vaildNodeContent.children[i], i);
                        }
                        _loop_1 = function (i) {
                            return __generator(this, function (_a) {
                                switch (_a.label) {
                                    case 0: return [4 /*yield*/, new Promise(function (resolve) {
                                            var item = cc.instantiate(_this.item);
                                            var is = item.getComponent(level_relationalGraphItemBase_1.default);
                                            creatorItem(item, i + curCount);
                                            item.setParent(_this.itemScrollView.content);
                                            _this.oneItemAddEvent(is.headNode);
                                            _this.scheduleOnce(resolve);
                                        })];
                                    case 1:
                                        _a.sent();
                                        return [2 /*return*/];
                                }
                            });
                        };
                        i = 0;
                        _a.label = 1;
                    case 1:
                        if (!(i < needCount)) return [3 /*break*/, 4];
                        return [5 /*yield**/, _loop_1(i)];
                    case 2:
                        _a.sent();
                        _a.label = 3;
                    case 3:
                        i++;
                        return [3 /*break*/, 1];
                    case 4: return [2 /*return*/];
                }
            });
        });
    };
    /**
     * 节点增加点击事件
     * @param item 节点
     */
    level_relationGraphBase.prototype.oneItemAddEvent = function (item) {
        item.on("touchmove", this.onTouchMoveItem, this);
        item.on("touchend", this.onTouchEndItem, this);
        item.on("touchcancel", this.onTouchEndItem, this);
    };
    /**
     * 节点移除事件
     * @param item 节点
     */
    level_relationGraphBase.prototype.oneItemRemoveEvent = function (item) {
        item.targetOff(this);
    };
    level_relationGraphBase.prototype.bgAddEvent = function () {
        this.bgNode.on('touchmove', this.onTouchMoveBg, this);
    };
    level_relationGraphBase.prototype.onTouchMoveBg = function (e) {
        e.stopPropagation();
        var touches = e.getTouches();
        if (touches.length == 1) {
            var maxXDistance = ((this.bgNode.width * this.bgNode.scale) - this.bgNode.parent.width) / 2;
            var maxYDistance = ((this.bgNode.height * this.bgNode.scale) - this.bgNode.parent.height) / 2;
            var delta = e.getDelta();
            var dx = delta.x;
            var dy = delta.y;
            if (this.bgNode.x + dx < -maxXDistance) {
                dx = 0;
            }
            if (this.bgNode.x + dx > maxXDistance) {
                dx = 0;
            }
            if (this.bgNode.y + dy < -maxYDistance) {
                dy = 0;
            }
            if (this.bgNode.y + dy > maxYDistance) {
                dy = 0;
            }
            this.bgNode.x += dx;
            this.bgNode.y += dy;
        }
    };
    /**
     * 移动item
     * @param e touch 事件
     */
    level_relationGraphBase.prototype.onTouchMoveItem = function (e) {
        var targetNode = e.target;
        var detal = e.getDelta();
        targetNode.x += detal.x;
        targetNode.y += detal.y;
    };
    /**
     * 移动item结束
     * @param e touch 事件
     */
    level_relationGraphBase.prototype.onTouchEndItem = function (e) {
        var targetNode = e.target;
        var parnetNode = targetNode.parent;
        var ps = parnetNode.getComponent(level_relationalGraphItemBase_1.default);
        var vaildNode = null;
        this.roleContent.children.forEach(function (n) {
            if (n.getBoundingBoxToWorld().intersects(targetNode.getBoundingBoxToWorld())) {
                vaildNode = n;
            }
        });
        //有合法节点
        if (vaildNode) {
            var vs = vaildNode.getComponent(level_relationalGraphRoleBase_1.default);
            var vid = vs.getRoleId();
            this.subStep();
            if (vid == ps.getRoleId()) {
                ps.headChangeBack();
                // 正确 进行矫正位置
                var pos = this.vaildNodeContent.convertToNodeSpaceAR(vaildNode.convertToWorldSpaceAR(cc.v2()));
                parnetNode.setParent(this.vaildNodeContent);
                parnetNode.setPosition(pos);
                parnetNode.removeComponent(cc.Button);
                // 正确一个
                if (this.vaildNodeContent.childrenCount == this.roleContent.childrenCount) {
                    // 胜利
                    this.Win(2);
                    return;
                }
            }
            else {
                // 返回之前的位置
                ps.headChangeBack();
            }
            this.checkStep();
        }
        else {
            // 返回之前的位置
            ps.headChangeBack();
        }
    };
    level_relationGraphBase.prototype.subStep = function () {
        this.stepCount--;
        this.refrenshStep();
    };
    level_relationGraphBase.prototype.checkStep = function () {
        if (this.stepCount <= 0) {
            // 没有步数了 提示需要增加步数 
        }
    };
    level_relationGraphBase.prototype.refrenshStep = function () {
        this.lbStepCount.string = this.stepCount.toString();
    };
    __decorate([
        property({ type: cc.ScrollView, tooltip: "所有角色的scrollView" })
    ], level_relationGraphBase.prototype, "itemScrollView", void 0);
    __decorate([
        property({ type: cc.Prefab, tooltip: "角色item" })
    ], level_relationGraphBase.prototype, "item", void 0);
    __decorate([
        property({ type: cc.Node, tooltip: "移动背景节点" })
    ], level_relationGraphBase.prototype, "bgNode", void 0);
    __decorate([
        property({ type: cc.Node, tooltip: "角色itemContent" })
    ], level_relationGraphBase.prototype, "roleContent", void 0);
    __decorate([
        property({ type: cc.Node, tooltip: "vaildNodeContent" })
    ], level_relationGraphBase.prototype, "vaildNodeContent", void 0);
    __decorate([
        property({ type: cc.Label, tooltip: "步数文本" })
    ], level_relationGraphBase.prototype, "lbStepCount", void 0);
    __decorate([
        property()
    ], level_relationGraphBase.prototype, "stepCount", void 0);
    level_relationGraphBase = __decorate([
        ccclass
    ], level_relationGraphBase);
    return level_relationGraphBase;
}(LevelBase_1.default));
exports.default = level_relationGraphBase;

cc._RF.pop();